package ai.people.netmon.telegram.service.impl;

import ai.people.netmon.framework.domain.telegram.vo.account.BusinessLeadsAccount;
import ai.people.netmon.framework.exception.enums.CommonEnum;
import ai.people.netmon.framework.model.request.PageKeyWordsRequest;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.utils.AssertUtils;
import ai.people.netmon.telegram.domain.request.SearchAccountChannelRequest;
import ai.people.netmon.telegram.mapper.AccountMapper;
import ai.people.netmon.telegram.service.AccountService;
import ai.people.netmon.utils.DesensitizationUtils;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * impl账户服务
 *
 * @author yuanqinglong
 * @date 2022/1/21 11:21
 */
@Service
public class AccountServiceImpl implements AccountService {


    @Autowired
    private AccountMapper accountMapper;


    /**
     * 通过用户名查询
     *
     * @param userId 用户id
     * @return {@link BusinessLeadsAccount}
     */
    @Override
    public BusinessLeadsAccount getAccountByUserId(Long userId) {
        BusinessLeadsAccount businessLeadsAccount = accountMapper.getAccountByUserId(userId);
        AssertUtils.isNotNull(businessLeadsAccount, CommonEnum.QUERY_NOT_FOUND);
        businessLeadsAccount.setPhone(DesensitizationUtils.hiddenPhone(businessLeadsAccount.getPhone()));
        return businessLeadsAccount;
    }

    /**
     * 业务帐户列表
     *
     * @param pageKeyWordsRequest 参数
     * @return {@link PageResponse}<{@link BusinessLeadsAccount}>
     */
    @Override
    public PageResponse<BusinessLeadsAccount> businessAccountList(PageKeyWordsRequest pageKeyWordsRequest) {
        long accountCount = accountMapper.businessAccountCount(pageKeyWordsRequest);
        if (accountCount == 0) {
            return PageResponse.builderEmpty();
        }
        List<BusinessLeadsAccount> businessLeadsAccountList = accountMapper.businessAccountList(pageKeyWordsRequest);
        businessLeadsAccountList.forEach(businessLeadsAccount -> businessLeadsAccount.setPhone(DesensitizationUtils.hiddenPhone(businessLeadsAccount.getPhone())));
        return PageResponse.builder(businessLeadsAccountList, accountCount);
    }


    /**
     * 搜索账户
     *
     * @param searchAccountRequest 搜索帐户请求
     * @return {@link PageResponse}<{@link BusinessLeadsAccount}>
     */
    @Override
    public PageResponse<BusinessLeadsAccount> searchAccount(SearchAccountChannelRequest searchAccountRequest) {
        List<String> fields = Optional.ofNullable(Strings.emptyToNull(searchAccountRequest.getSelectFields())).map(s -> Arrays.asList(s.split(","))).orElse(Collections.emptyList());
        long accountCount = accountMapper.searchAccountCount(fields, searchAccountRequest.getSearchKeywords());
        if (accountCount == 0) {
            return PageResponse.builderEmpty();
        }
        List<BusinessLeadsAccount> searchAccountList = accountMapper.searchAccountList(searchAccountRequest.getPageNo(), searchAccountRequest.getPageSize(), fields, searchAccountRequest.getSearchKeywords());
        searchAccountList.forEach(businessLeadsAccount -> businessLeadsAccount.setPhone(DesensitizationUtils.hiddenPhone(businessLeadsAccount.getPhone())));
        // TODO: 2022/1/24 加入分析标识
        return PageResponse.builder(searchAccountList, accountCount);
    }
}
